package com.cheng.io.writer;

import java.io.*;
import java.nio.charset.Charset;

/**
 * 解决FileWriter写入文件时乱码的问题
 * 使用OutputStreamWriter设置编码字符集，解决写入文件时乱码的问题，转换字节输出流为字符输出流
 * OutputStreamWriter是一个字符输出流
 * FileWriter是一个包装流
 */
public class FileWriterEncodingTest01 {
    public static void main(String[] args) {
        try (
                // OutputStreamWriter(OutputStream out, String charsetName) 需要传入一个节点流和指定字符集
                // OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("io/resources/特性1.txt", true), "GBK");
                // 使用多态特性简化对象创建
                Writer outputStreamWriter = new FileWriter("io/resources/特性1.txt", Charset.forName("GBK"), true)
        ) {
            outputStreamWriter.write("\nIO流");
            outputStreamWriter.flush();

            System.out.println("successful..");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
